﻿using System;
using System.Collections;

namespace UniRx.Operators
{
	// Token: 0x020003EF RID: 1007
	internal class FromCoroutineObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x0600149F RID: 5279 RVA: 0x0005D130 File Offset: 0x0005B530
		public FromCoroutineObservable(Func<IObserver<T>, CancellationToken, IEnumerator> coroutine) : base(false)
		{
			this.coroutine = coroutine;
		}

		// Token: 0x060014A0 RID: 5280 RVA: 0x0005D140 File Offset: 0x0005B540
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			FromCoroutineObservable<T>.FromCoroutine arg = new FromCoroutineObservable<T>.FromCoroutine(observer, cancel);
			BooleanDisposable booleanDisposable = new BooleanDisposable();
			CancellationToken arg2 = new CancellationToken(booleanDisposable);
			MainThreadDispatcher.SendStartCoroutine(this.coroutine(arg, arg2));
			return booleanDisposable;
		}

		// Token: 0x04000BF4 RID: 3060
		private readonly Func<IObserver<T>, CancellationToken, IEnumerator> coroutine;

		// Token: 0x020003F0 RID: 1008
		private class FromCoroutine : OperatorObserverBase<T, T>
		{
			// Token: 0x060014A1 RID: 5281 RVA: 0x0005D176 File Offset: 0x0005B576
			public FromCoroutine(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x060014A2 RID: 5282 RVA: 0x0005D180 File Offset: 0x0005B580
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x060014A3 RID: 5283 RVA: 0x0005D1BC File Offset: 0x0005B5BC
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060014A4 RID: 5284 RVA: 0x0005D1F4 File Offset: 0x0005B5F4
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}
		}
	}
}
